<script setup lang="ts">
import EntryBg from '@/components/entry/EntryBg.vue';
import { ref } from 'vue';
import { User, Lock } from '@element-plus/icons-vue';
import { ElMessage } from 'element-plus';
import { userService } from '@/service';
import { useRouter } from 'vue-router';

const router = useRouter();

const userName = ref('');
const password = ref('');
const loading = ref(false);

async function submit() {
  if (!userName.value || !password.value) {
    ElMessage.error('用户名或密码为空');
    return;
  }
  loading.value = true;
  const result = await userService.login({
    userName: userName.value,
    password: password.value,
  });
  if (result.success) {
    await router.push({name: 'home'});
  } else {
    ElMessage.error(result.errorMsg || '登录失败');
  }
  loading.value = false;
}

</script>

<template>
  <EntryBg />
  <div class="entry-view">
    <div class="entry-view-form">
      <div class="login-form">
        <div class="login-form-item">
          <el-input v-model="userName" type="text" placeholder="用户名" size="large" :prefix-icon="User" @keyup.enter="submit" />
        </div>
        <div class="login-form-item">
          <el-input v-model="password" type="password" placeholder="密码" show-password size="large" :prefix-icon="Lock" @keyup.enter="submit" />
        </div>
        <div class="login-form-item">
          <el-button type="primary" size="large" :style="{ width: '100%', marginTop: '24px' }" :loading="loading" @click="submit"> 登录 </el-button>
        </div>
      </div>
    </div>
    <div class="copyright">
      Copyright © 2025 深圳灵码科技有限公司 All Rights Reserved.
      <a href="https://beian.miit.gov.cn/" target="_blank"> 粤ICP备18059311号-3</a>
    </div>
  </div>
</template>

<style lang="less" scoped>
.entry-view {
  height: 100%;
  padding-top: 12%;
  position: relative;


  .entry-view-form {
    margin: 0 auto;
    width: 360px;
    min-height: 260px;
    .login-form {
      margin-top: 60px;
      .login-form-item {
        margin-bottom: 24px;
      }
    }
  }

  .copyright {
    position: absolute;
    bottom: 5px;
    left: 0;
    right: 0;
    font-size: 14px;
    font-weight: 400;
    color: #3f3b3b;
    line-height: 20px;
    text-align: center;
    a {
      color: var(--el-color-primary);
    }
  }
}
</style>
